package net.cn_su.sharpeyes.utils;

/**
 * 此工具来定位，获得经纬度
 *
 * @author HrcmChan
 */

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;


/**
 * 需要在manifest配置以下信息
 * <p/>
 * <p/>
 * <!-- 百度定位相关 --> <service android:name="com.baidu.location.f"
 * android:enabled="true" android:process=":remote" > </service>
 * <p/>
 * <meta-data android:name="com.baidu.lbsapi.API_KEY"
 * android:value="pVGQB93dVZGxChvOewO7oMeF" />
 *
 * @author Administrator
 */
public class MyLocationUtil {

    private float lat; // 纬度
    private float lon; // 经度
    private String address; //位置
    private MyLocationListener locationListener;
    private int errorCode;
    private LocationClient mLocationClient;


    private int maxCount;


    public MyLocationUtil(Context context, MyLocationListener locationListener, int maxCount) {
        mLocationClient = new LocationClient(context.getApplicationContext());
        this.locationListener = locationListener;
        this.maxCount = maxCount;
        Getcoordinatr();
    }


    /**
     * 访问网络，开始点位
     */
    public void Getcoordinatr() {

        //设置定位参数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
        option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值bd09ll
        option.setScanSpan(2000);// 设置发起定位请求的间隔时间为1100ms
        option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(listener);

        mLocationClient.start();

    }


    public void start() {
        if (mLocationClient != null && !mLocationClient.isStarted()) {
            mLocationClient.start();
        }
    }

    public void stop() {
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }


    int count = 0;
    // 监听
    BDLocationListener listener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // 错误返回码
            errorCode = location.getLocType();
            //   Logger.v("errorCode==" + errorCode);
            //  Logger.v("lat==" + location.getLatitude());
            lat = (float) location.getLatitude();

            if (lat > 0&&location.getCity()!=null) {
                if (mLocationClient.isStarted()) {
                    locationListener.sussessLocation((float) location.getLatitude(), (float) location.getLongitude(), location);
                    mLocationClient.stop();
                }
                count = 0;

            } else {

                if (count == maxCount) {
                    if (mLocationClient.isStarted()) {
                        mLocationClient.stop();
                    }
                    count = 0;
                    locationListener.errorLocation();
                }

            }

            count++;

        }
    };


    public interface MyLocationListener {


        void sussessLocation(float lat, float lon, BDLocation location);

        void errorLocation();


    }


}
